/**
 *@file      lmf_queue.c
 *@brief     Logic moudle function of Queue
 *@author    zxf 2018-12-17
 *@version   V1.0.0
 */
#include "DSM.h"


/* Private define -----------------------------------------------------------*/
/* Private typedef ----------------------------------------------------------*/
/* Private variables --------------------------------------------------------*/
/* Private functions declaration --------------------------------------------*/
/* Interface functions ------------------------------------------------------*/



/**
  *@brief  queue create
  */
void DSM_QCreate(DSM_Q_t *q, void *array, u16 size, u8 unit)
{
    q->size = size;
    q->item = 0;
    q->unit = unit;
    q->start = array;
    q->end = (u8*)array + size*unit;
    q->in = q->start;
    q->out = q->start;
}


u32 DSM_QIn(DSM_Q_t *q, void *d)
{
    if(q == NULL)
        return 1;
    
    if(q->item >= q->size)
        return 2;
    
    memcpy(q->in, d, q->unit);
    q->in += q->unit;
    q->item++;
    if(q->in == q->end)
        q->in = q->start;
    
    return q->item;
}

u32 DSM_QOut(DSM_Q_t *q)
{
    u32 rval;
    
    if(q == NULL)
        return 0;
    
    if(q->item == 0)
        return 0;
    
    memcpy(&rval, q->out, q->unit);
    q->out += q->unit;
    q->item--;
    if(q->item == 0)
    {
        q->in = q->start;
        q->out = q->start; 
    }
    else
    {
        if(q->out == q->end)
            q->out = q->start;
    }
    
    return rval;
}

u8 DSM_QIsEmpty(DSM_Q_t *q)
{
    if(q->item == 0)
        return TRUE;
    
    return FALSE;
}

void DSM_Clear(DSM_Q_t *q)
{
    if(q)
    {
        memset(q->start, 0, q->size*q->unit);
        q->item = 0;
        q->in = q->start;
        q->out = q->start;
    }
}

/* Private functions area ---------------------------------------------------*/



